<?php

namespace app\nideshop\controller;

use app\nideshop\service\Token;
use think\Db;

/**
 * 专题
 */
class Address extends Base
{
    protected $datas;

    public function listAction()
    {
        $userId = Token::unlock_url($_SERVER['HTTP_X_NIDESHOP_TOKEN']);
        $addressList = Db::name('address')
        ->where('user_id', $userId)
        ->select();
        
        for ($itemKey = 0; $itemKey < count($addressList); $itemKey++)
        {
            $addressItem = $addressList[$itemKey];
            $province_name = Db::name('region')
            ->where('id', $addressItem['province_id'])
            ->field('name')
            ->value('name');
            $addressList[$itemKey]['province_name'] = $province_name;

            $city_name = Db::name('region')
            ->where('id', $addressItem['city_id'])
            ->field('name')
            ->value('name');
            $addressList[$itemKey]['city_name'] = $city_name;

            $district_name = Db::name('region')
            ->where('id', $addressItem['district_id'])
            ->field('name')
            ->value('name');
            $addressList[$itemKey]['district_name'] = $district_name;
            $addressList[$itemKey]['full_region'] = $province_name.$city_name.$district_name;
        }
        return json([
            'errno' => 0,
            'errmsg' => '成功',
            'data' => $addressList
        ]);
    }

    public function detailAction()
    {
        $this->datas = $this->params;
        $addressId = (int) $this->datas['id'];
        $userId = Token::unlock_url($_SERVER['HTTP_X_NIDESHOP_TOKEN']);
        $addressInfo = Db::name('address')
        ->where('user_id', $userId)
        ->where('id', $addressId)
        ->find();

        // 没有就直接返回
        if (empty($addressInfo))
        {
            return json([
                'errno' => 0,
                'errmsg' => '成功',
                'data' => new static
            ]);
        }

        $province_name = Db::name('region')
        ->where('id', $addressInfo['province_id'])
        ->field('name')
        ->value('name');
        $addressInfo['province_name'] = $province_name;

        $city_name = Db::name('region')
        ->where('id', $addressInfo['city_id'])
        ->field('name')
        ->value('name');
        $addressInfo['city_name'] = $city_name;

        $district_name = Db::name('region')
        ->where('id', $addressInfo['district_id'])
        ->field('name')
        ->value('name');
        $addressInfo['district_name'] = $district_name;
        $addressInfo['full_region'] = $province_name.$city_name.$district_name;
        return json([
            'errno' => 0,
            'errmsg' => '成功',
            'data' => empty($addressInfo) ? new static : $addressInfo
        ]);
    }

    /**
     * 添加或更新收货地址
     */
    public function saveAction() 
    {
        $this->datas = $this->params;
        $name = $this->datas['name'];
        $mobile = $this->datas['mobile'];
        $province_id = $this->datas['province_id'];
        $city_id = $this->datas['city_id'];
        $district_id = $this->datas['district_id'];
        $address = $this->datas['address'];
        $userId = Token::unlock_url($_SERVER['HTTP_X_NIDESHOP_TOKEN']);
        $addressId = '';
        $is_default = 0;
        if (array_key_exists('id', $this->datas)) 
        {
            $addressId = $this->datas['id']; 
        }
        if (array_key_exists('is_default', $this->datas)) 
        {
            $is_default = $this->datas['is_default'];
        }
        
        $addressData = [
            'name' => $name,
            'mobile' => $mobile,
            'province_id' => $province_id,
            'city_id' => $city_id,
            'district_id' => $district_id,
            'address' => $address,
            'user_id' => $userId,
            'is_default' => $is_default == true ? 1 : 0
        ];
        if (empty($addressId))
        {
            $addressId = Db::name('address')
            ->insertGetId($addressData);
        }
        else
        {
            Db::name('address')
            ->where('id', $addressId)
            ->where('user_id', $userId)
            ->update($addressData);
        }

        if ($is_default == true) 
        {
            Db::name('address')
            ->where('id', '<>', $addressId)
            ->where('user_id', $userId)
            ->update([
                'is_default' =>  0
            ]);
        }

        $addressInfo = Db::name('address')
        ->where('id', $addressId)
        ->find();
        return json([
            'errno' => 0,
            'errmsg' => '成功',
            'data' => $addressInfo
        ]);
    }

    public function deleteAction()
    {
        $this->datas = $this->params;
        $addressId = $this->datas['id'];
        $userId = Token::unlock_url($_SERVER['HTTP_X_NIDESHOP_TOKEN']);

        Db::name('address')
        ->where('id', $addressId)
        ->when('user_id', $userId)
        ->delete();

        return json([
            'errno' => 0,
            'errmsg' => '成功',
            'data' => new static
        ]);
    }
}
